﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClusterAggregation.Datum
{
    /**
     * simple matrix based distance function
     */
    public class CMatrixBasedDistance : ISimilarity
    {
        /** the matrix by which we compute values */
        private double[,] _distanceMatrix;

        /** 
         * basic constructor, saves the distance mat
         * @param arg (double[,]) [IN] 
         */
        public CMatrixBasedDistance(double[,] arg)
        {
            _distanceMatrix = arg;
        }

        public double[,] getMatrix(AData[] arg)
        {
            return getFullMatrix(arg);
        }
        public double[,] getFullMatrix(AData[] arg)
        {
            int lng = (int)Math.Sqrt(_distanceMatrix.Length);
            double[,] tmp = new double[lng, lng];

            // copy and allocate
            for (int i = 0; i < lng; i++)
            {
                for (int j = 0; j < lng; j++)
                    tmp[i,j] = _distanceMatrix[i,j];
            }

            return tmp;
        }

        public double similarity(AData a, AData b)
        {
            int i = a.id > b.id ? a.id : b.id; // max 
            int j = a.id < b.id ? a.id : b.id; // min
            return _distanceMatrix[i,j];
        }
    }
}
